package com.example.mybatisplusclass2.controller;

import com.example.mybatisplusclass2.entity.Student;
import com.example.mybatisplusclass2.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *1.对相应的表使用自动化工具生成代码
 * 2.把学习过的StudentController里面的方法复制到新生成的***Controller里面，注意修改为RestController为Controller
 * 3.更改为对应表的信息
 * 4.复制templates里面对应的html页面，根据对应的表信息的属性进行页面的修改
 * 5.强调一点，Controller里面和html页面之间值的对应关系
 * odel.addAttribute("StudentCards", studentCards);
 * "StudentCards"必须和 <tr th:each="studentCard : ${StudentCards}">
 * 里面${StudentCards}的名字相同！！！
 * @author coolwen
 * @since 2024-03-19 15:00:17
 */
@Controller
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    IStudentService studentService;
    @GetMapping("/student01")
    @ResponseBody

    public List<Student> getStudents() {
        return studentService.list();
    }

    @GetMapping("/student01/{id}")
    @ResponseBody
    public Optional<Student> getStudentById(@PathVariable Integer id) {
        // 调用studentService的getOptById方法获取学生信息
        Optional<Student> student = studentService.getOptById(id);
        if (student == null) {
            // 如果找不到学生，可以抛出一个异常或返回特定的错误响应
            throw new ResourceNotFoundException("Student not found with id: " + id);
        }
        // 返回学生对象，Spring Boot会自动将其转换为JSON格式
        return student;
    }


    @GetMapping("/students")
    public String listStudents(Model model) {
        List<Student> students = studentService.list();
        model.addAttribute("students", students);
        return "students/list"; // 返回的视图名
    }

    @GetMapping("/demo2")
    public String demo2(
            @RequestParam("age") String age){
        System.out.println("age="+age);
        return age;
    }

    @GetMapping("/{id}")
    public String getStudentById(@PathVariable("id") Integer id, Model model) {
        Student student = studentService.getBalanceById(id);
        model.addAttribute("student", student);
        return "students/studentDetails";
    }

    @GetMapping ("/studentadd")
    public String addStudentForm(Model model) {
        model.addAttribute(new Student());
        return "/students/studentadd";
    }

    @GetMapping("/zhu")
    public String getHotelPage() {
        return "zhu"; // 返回hotel.html视图
    }


    @PostMapping("/studentadd")
    public String savestudent(@ModelAttribute("student") Student student) {
        log.debug(student.toString());
        studentService.save(student);
        return "redirect:/student/students";

    }

    @GetMapping("delete/{id}")
    public String deleteStudent(@PathVariable Integer id) {
        studentService.removeById(id);
        return "redirect:/student/students";
    }

    @GetMapping("/update/{id}")
    public String editUpdateForm(@PathVariable Integer id, Model model) {
        Student student = studentService.getById(id); // 假设有这个方法获取学生信息
        model.addAttribute("student", student);
        return "students/studentUpdate";
    }

    @PostMapping("/update/{id}")
    public String updateStudent(@ModelAttribute("student") Student student) {
        log.debug(student.toString());
        studentService.updateById(student);
        return "redirect:/student/students";
    }




//    @GetMapping("list")
//    public String List(){
//        List<Student> students=studentService.list();
////        log.debug(studentService.list().toString());
//        return "";

//    }
}
